package com.example.duanjunjie.learn;

import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.duanjunjie.learn.http.fetcher.WeatherFetcher;
import com.example.duanjunjie.learn.http.model.Weather;
import com.example.duanjunjie.learn.http.request.WeatherRequest;
import com.example.duanjunjie.learn.intentservice.MyIntentService;
import com.example.duanjunjie.learn.jobscheduler.MyJobService;
import com.example.duanjunjie.learn.proxy.Person;
import com.example.duanjunjie.learn.proxy.Student;
import com.example.duanjunjie.learn.proxy.StudentProxy;
import com.example.duanjunjie.learn.thread.MyRunnable;
import com.example.duanjunjie.learn.wk.WKActivity;
import com.example.duanjunjie.learn.wk.WKNoticeManager2;

import java.lang.reflect.Proxy;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject;
import rx.subscriptions.CompositeSubscription;

public class MainActivity extends AppCompatActivity {

    private Button textView;
    private CompositeSubscription cs = new CompositeSubscription();
    private PublishSubject<Boolean> weatherPublishSubject = PublishSubject.create();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textView);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //startActivity(new Intent(MainActivity.this,KotlinTestActivity.class));
                //weatherPublishSubject.onNext(true);
                //proxyTest();
                //intentServiceTest();
                //threadTest();
                // startActivity(new Intent(MainActivity.this, RoundActivity.class));
                // startActivity(new Intent(MainActivity.this, RecycleViewActivity.class));
                //jobSchedulerTest();
                startActivity(new Intent(MainActivity.this, WKActivity.class));
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent(this, MyJobService.class);
        startService(intent);
    }

    @Override
    protected void onDestroy() {
        WKNoticeManager2.getInstance().release();
        super.onDestroy();
    }

    private void jobSchedulerTest() {
        JobInfo.Builder builder = new JobInfo.Builder(1, new ComponentName(getPackageName(), MyJobService.class.getName()));
        builder.setMinimumLatency(30 * 1000);
        JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        int result = jobScheduler.schedule(builder.build());
        Log.i("djj", "result" + result);
    }

    public void threadTest() {
        MyRunnable runnable = new MyRunnable();
        try {
            Thread.sleep(1000);
            runnable.sendMsg();
            runnable.stop();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    void intentServiceTest() {
        Intent intent = new Intent(this, MyIntentService.class);
        startService(intent);
    }


    void proxyTest() {
        StudentProxy proxy = new StudentProxy(new Student());
        Person p = (Person) Proxy.newProxyInstance(Person.class.getClassLoader(), new Class[]{Person.class}, proxy);
        p.eat("fan");
    }

    void httpTest() {
        final WeatherFetcher weatherFetcher = new WeatherFetcher();
        final WeatherRequest req = new WeatherRequest();
        req.cityname = "北京";
        req.key = "471421e50b5aa0d5525ac70fd8effe73";
        cs.add(weatherPublishSubject.flatMap(new Func1<Boolean, Observable<Weather>>() {
            @Override
            public Observable<Weather> call(Boolean b) {
                return weatherFetcher.fetch(req).subscribeOn(Schedulers.io());
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Weather>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        Log.i("djj", "onStart");
                    }

                    @Override
                    public void onCompleted() {
                        Log.i("djj", "onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("djj", e.toString());
                    }

                    @Override
                    public void onNext(Weather weather) {
                        Log.i("djj", weather.toString());
                    }
                }));
    }


}
